package com.example.day10_zuoye.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.day10_zuoye.R;
import com.example.day10_zuoye.fragmnet.FourFragment;
import com.example.day10_zuoye.fragmnet.OneFragment;
import com.example.day10_zuoye.fragmnet.ThreeFragment;
import com.example.day10_zuoye.fragmnet.TwoFragment;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = findViewById(R.id.group);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        if(oneFragment != null){
            transaction.show(oneFragment);
        }else{
            oneFragment = new OneFragment();
            transaction.add(R.id.frame,oneFragment);
        }
        transaction.commit();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                hideAll();
                switch (checkedId){
                    case R.id.rb1:
                            {
                                FragmentManager supportFragmentManager = getSupportFragmentManager();
                                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                                if(oneFragment != null){
                                    transaction.show(oneFragment);
                                }else{
                                    oneFragment = new OneFragment();
                                    transaction.add(R.id.frame,oneFragment);
                                }
                                transaction.commit();
                            }

                        break;
                    case R.id.rb2:
                    {
                        FragmentManager supportFragmentManager = getSupportFragmentManager();
                        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                        if(twoFragment != null){
                            transaction.show(twoFragment);
                        }else{
                            twoFragment = new TwoFragment();
                            transaction.add(R.id.frame,twoFragment);
                        }
                        transaction.commit();
                    }

                    break;
                    case R.id.rb3:
                    {
                        FragmentManager supportFragmentManager = getSupportFragmentManager();
                        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                        if(threeFragment != null){
                            transaction.show(threeFragment);
                        }else{
                            threeFragment = new ThreeFragment();
                            transaction.add(R.id.frame,threeFragment);
                        }
                        transaction.commit();
                    }

                    break;
                    case R.id.rb4:
                    {
                        FragmentManager supportFragmentManager = getSupportFragmentManager();
                        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                        if(fourFragment != null){
                            transaction.show(fourFragment);
                        }else{
                            fourFragment = new FourFragment();
                            transaction.add(R.id.frame,fourFragment);
                        }
                        transaction.commit();
                    }

                    break;
                }
            }
        });

    }

    public void hideAll(){
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        if(oneFragment != null){
            transaction.hide(oneFragment);
        }
        if(twoFragment != null){
            transaction.hide(twoFragment);
        }
        if(threeFragment != null){
            transaction.hide(threeFragment);
        }
        if(fourFragment != null){
            transaction.hide(fourFragment);
        }
        transaction.commit();
    }



}
